package org.wj.algorithm

import org.apache.spark.{SparkConf, SparkContext}

object SecondSort {


  def main(args: Array[String]): Unit = {

    val conf = new SparkConf()
    conf.setAppName(this.getClass.getName).setMaster("local")
    val sc = new SparkContext(conf)

    sc.textFile("hdfs://localhost:9000/temperature.txt").map(line => {
      val cols = line.split(",")
      (cols(cols.length-1), new SecondSort(cols(0), cols(1)))
    }).sortByKey().map(t=> (t._2, t._1.toString)).sortByKey().foreach(println(_))

  }

  class SecondSort(private var year:String,private var mouth:String) extends Ordered[SecondSort]{
    override def compare(that: SecondSort): Int = if(year == that.year) mouth.compare(that.mouth) else year.compare(that.year)
    override def toString: String = year + "-" + mouth
  }

}
